﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Assets.Script.Config;

//子弹实体类
public class BattleEntityBullet : BattleEntity
{
    public GameObject m_pGameObj;
    public MoveComp m_pMoveComp;
    public SkillAction m_pSkillAction;
    public Dmg m_pDmg;

    public override void OnCreate(params object[] args)
    {
        m_pGameObj = new GameObject();
        m_pGameObj.name = "Entity_Bullet_" + Id.ToString();
        Vector3 targetPos = (Vector3)args[0];
        m_pSkillAction = (SkillAction)args[1];
        m_pGameObj.transform.position= (Vector3)args[2];
        m_pGameObj.transform.localEulerAngles = (Vector3)args[3];
        m_pDmg = (Dmg)args[4];
 
        if (m_pSkillAction.action3EffId != 0)
            BattleEffectMgr.Instance.CreateEffectObj(m_pSkillAction.action3EffId, Id, m_pGameObj.transform, Vector3.zero);

        m_pMoveComp = new MoveComp(m_pGameObj.transform, this);

        HeroEff config = ConfigManager.Instance.heroEffLoader.GetInfoById(m_pSkillAction.action3EffId);
        m_pMoveComp.UniformMove(config.speed, targetPos);
    }

    public override void OnUpdate(float deltaTime)
    {
        base.OnUpdate(deltaTime);
        m_pMoveComp.OnUpdate(deltaTime);
    }

    public override bool HandleMessage(MessageBody body)
    {
        if(body.msgId == MessageId.Arrive)
        {
            //子弹到达了目标点
            m_pMoveComp.StopMove();

            if (m_pSkillAction.hitEffId!= 0)
                BattleEffectMgr.Instance.CreateEffectObj(m_pSkillAction.hitEffId, m_pGameObj.transform.position);

            if(m_pSkillAction == null)
            {
                Debug.Log("m_pSkillAction is null");
            }
            else
            {
                Debug.Log("m_pSkillAction is not null");
            }


            //向父级发送消息
            if (ParentNotNull())
            {
                EntityMessageDispatcher.Instance.SendMessage(MessageId.BulletArrive, Id, GetParent().Id,0,m_pDmg);
            }

            BattleEntityFactory.Instance.DestroyEntity(Id);
            BattleEffectMgr.Instance.DelEntityAllEffObj(Id);
        }

        return false;
    }

    public override void OnDestroy()
    {
        GameObject.DestroyObject(m_pGameObj);
    }
}
